/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects;

import static org.junit.Assert.*;

import org.junit.Test;

import org.latestbit.smartobjects.core.SessionId;
import org.latestbit.smartobjects.network.EndpointLocation;
import org.latestbit.smartobjects.testmodel.*;

public class SimpleInterfaceFactoryTest {
	
	@Test
	public void testCreateSimpleInterfaceFactory() throws Exception {
		ISmartBus bus = new SmartBus();
		final String location = "tcp://localhost:7777";
		
		SimpleFactorySyncSkeleton simpleSyncServer = new SimpleFactorySyncSkeleton (
				new SimpleInterfaceFactorySyncServer()
		);
		simpleSyncServer.publish ( bus, new EndpointLocation(bus, location) );
		
		ISimpleFactory clientFactory = new SimpleFactory();
		clientFactory.resolve( bus, new EndpointLocation(bus, location) );
		
		ISimpleInterface simpleClient = clientFactory.createSimpleInterface();
		
		String result = simpleClient.helloWorld("World");
		assertEquals ( "Hello World", result );
		simpleClient.manualDestroy();
		
		
		simpleClient.destroy();
		clientFactory.destroy();
		simpleSyncServer.destroy();
		bus.destroy();
	}

	@Test
	public void testPutSimpleInterfaceFactory() throws Exception {
		ISmartBus bus = new SmartBus();
		final String location = "tcp://localhost:7777";
		
		SimpleFactorySyncSkeleton simpleSyncServerFactory = new SimpleFactorySyncSkeleton (
				new SimpleInterfaceFactorySyncServer()
		);
		simpleSyncServerFactory.publish ( bus, new EndpointLocation(bus, location) );
		
		SimpleFactory clientFactory = new SimpleFactory();
		clientFactory.resolve( bus, new EndpointLocation(bus, location) );
		
		SimpleInterfaceSyncSkeleton simpleInterfaceServer = 
			new SimpleInterfaceSyncSkeleton(new SimpleInterfaceSyncServer());
		
		String result = clientFactory.putSimpleInterface( simpleInterfaceServer );
		assertEquals ( "Hello World", result );
		
		simpleInterfaceServer.destroy();
		clientFactory.destroy();
		simpleSyncServerFactory.destroy();
		bus.destroy();
	}
	
	@Test
	public void testSimpleInterfaceFactorySessionId() throws Exception {
		ISmartBus bus = new SmartBus();
		final String location = "tcp://localhost:7777";
		
		SimpleInterfaceFactorySyncServer serverImpl = new SimpleInterfaceFactorySyncServer();
		SimpleFactorySyncSkeleton simpleSyncServerFactory = new SimpleFactorySyncSkeleton (
				serverImpl
		);
		simpleSyncServerFactory.publish ( bus, new EndpointLocation(bus, location) );
		
		SimpleFactory clientFactory = new SimpleFactory();
		clientFactory.resolve( bus, new EndpointLocation(bus, location) );
				
		SessionId<String> result = clientFactory.createSimpleSession() ;
		clientFactory.continueSession(result);
		assertEquals ( result.getId(), serverImpl.getSessionId() );
		
		clientFactory.destroy();
		simpleSyncServerFactory.destroy();
		bus.destroy();
	}
	
}
